﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Command.HelpClass;
using czc;
using Guestbook.Biz;
using Guestbook.IBiz;

namespace WebApp.Controllers
{
    public class RoleManagementController : Controller
    {

        public IRolesBiz groupsBiz = new RolesBiz();
        //
        // GET: /RoleManagement/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetList()
        {

            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            ////字段排序
            //String sortField = Request["sortField"];
            //String sortOrder = Request["sortOrder"];
            PageClass pc = new PageClass();
            pc.sys_Fields = "*";
            pc.sys_Key = "RoleId";
            pc.sys_PageIndex = pageIndex ;
            pc.sys_PageSize = pageSize;
            pc.sys_Table = "Roles";
            pc.sys_Where = "1=1";
            pc.sys_Order = "RoleId";


            List<Roles> list2 = groupsBiz.GetPagingData<Roles>(pc);
            Dictionary<string, object> dic = new Dictionary<string, object>();
           

            // var mql = ControlButtonsSet.ControlId.NotEqual("");
            dic.Add("rows", list2);
            dic.Add("total", pc.RCount);

            return Json(dic, JsonRequestBehavior.AllowGet); ;
        }

        public JsonResult AddRole(Roles RolesModle)
        {
            Random rnd = new Random();
            Roles rol = new Roles();
            rol.RoleId = rnd.Next(10, 9999).ToString();
            rol.RoleName = RolesModle.RoleName;
            rol.RoleDescription = RolesModle.RoleDescription;
            rol.RoleOrder = RolesModle.RoleOrder;
            groupsBiz.Add(rol);
            return Json("ok", JsonRequestBehavior.AllowGet); 
        }
        public JsonResult GetRoleInfos(string ID)
        {
            var mql2 = RolesSet.SelectAll().Where(RolesSet.RoleId.Equal(ID));
            Roles Rmodel = groupsBiz.GetEntity(mql2);
          //  groupsBiz.Add(rol);
            return Json(Rmodel, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateRoleInfo(Roles RolesModle)
        {
            Roles rol = new Roles();
          //  rol.RoleId = rnd.Next(10, 9999).ToString();
            rol.RoleName = RolesModle.RoleName;
            rol.RoleDescription = RolesModle.RoleDescription;
            rol.RoleOrder = RolesModle.RoleOrder;
            rol.WhereExpression = RolesSet.RoleId.Equal(RolesModle.RoleId);
            //  spmodel.GroupId = GroupId;
            groupsBiz.Update(rol);
            return Json("ok", JsonRequestBehavior.AllowGet); 
        }

        public JsonResult DeleteRole(string ID)
        {
             
            var mql2 = RolesSet.RoleId.Equal(ID);
            int f = groupsBiz.Remove(mql2);
            return Json("OK", JsonRequestBehavior.AllowGet); 

        }

    }
}
